package com.yy.ant.chat.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yy.ant.chat.entity.Flockrefuser;
import com.yy.ant.chat.entity.Friends;
import com.yy.ant.chat.entity.Message;
import com.yy.ant.chat.service.IFriendsService;
import com.yy.ant.chat.service.IMessageService;
import com.yy.ant.sys.entity.User;
import com.yy.model.ResultMsg;
import com.yy.model.enums.ResultEnum;
import com.yy.utils.SessionUtil;

/**
 * <p>
 * 好友关系表 前端控制器
 * </p>
 *
 * @author liyingying
 * @since 2020-01-06
 */
@RestController
@RequestMapping("/chat/friends")
public class FriendsController {

	@Autowired
	private IFriendsService iFriendsService;
	@Autowired
	private IMessageService iMessageService;
	
	
	/**
	 * 同意添加好友
	 *  @param id //消息的ID
	 * @param uid //对方用户ID
	 * @param group //我设定的好友分组
	 * @return
	 */
	@RequestMapping(value = "/agreeFriend")
	public ResultMsg agreeFriend(@RequestParam(value = "id") String id,@RequestParam(value = "uid") String uid,
			@RequestParam(value = "group") String group) {
		ResultMsg resultMsg=new ResultMsg();
		Message message=iMessageService.getById(id);
		//先查询自己跟对方是否是好友
		if(isFirend(message.getUid().toString(),message.getFroms().toString())) {
			resultMsg.setMsg(ResultEnum.error_haoyou_failed.getMsg());
			return resultMsg;
		}
		Friends friends=new Friends();
		friends.setFriend(message.getFroms());
		friends.setGroupid(message.getFromGroup());
		friends.setMe(message.getUid());
		boolean b=iFriendsService.save(friends);
		Friends friends1=new Friends();
		friends1.setFriend(message.getUid());
		friends1.setGroupid(Integer.valueOf(group));
		friends1.setMe(message.getFroms());
		boolean b1=iFriendsService.save(friends1);
		if(b&&b1) {
			resultMsg.setSuccess(true);
			resultMsg.setMsg(ResultEnum.sucess_create_failed.getMsg());
		}else {
			resultMsg.setMsg(ResultEnum.error_create_failed.getMsg());
		}
		Message message1=new Message();
		message1.setId(Long.valueOf(id));
		message1.setTypes(3);
		message1.setReada(1);
		iMessageService.updateById(message1);
		return resultMsg;
	}
	
	
	
	/**
	 *加入好友
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/add")
	public ResultMsg add(@RequestParam(value = "id") String id) {
		ResultMsg resultMsg=new ResultMsg();
		Message message=iMessageService.getById(id);
		Friends friends=new Friends();
		friends.setFriend(message.getUid());
		friends.setGroupid(message.getFromGroup());
		friends.setMe(message.getFroms());
		if(isFirend(message.getUid().toString(),message.getFroms().toString())) {
			resultMsg.setMsg(ResultEnum.error_haoyou_failed.getMsg());
			return resultMsg;
		}
		boolean b=iFriendsService.save(friends);
		if (b) {
			resultMsg.setSuccess(true);
			resultMsg.setMsg(ResultEnum.sucess_create_failed.getMsg());
		}else {
			resultMsg.setMsg(ResultEnum.error_create_failed.getMsg());
		}
		
		return resultMsg;
	}
	
	/**
	 *加别人为好友
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/addfriends")
	public ResultMsg addfriends(@RequestParam(value = "id") String id,@RequestParam(value = "gid") String gid) {
		ResultMsg resultMsg=new ResultMsg();
		Message message=iMessageService.getById(id);
		Friends friends=new Friends();
		friends.setFriend(message.getFroms());
		friends.setGroupid(Integer.valueOf(gid));
		friends.setMe(message.getUid());
		if(isFirend(message.getFroms().toString(),message.getUid().toString())) {
			resultMsg.setMsg(ResultEnum.error_haoyou_failed.getMsg());
			return resultMsg;
		}
		boolean b=iFriendsService.save(friends);
		if (b) {
			resultMsg.setSuccess(true);
			resultMsg.setMsg(ResultEnum.sucess_create_failed.getMsg());
		}else {
			resultMsg.setMsg(ResultEnum.error_create_failed.getMsg());
		}
		
		return resultMsg;
	}
	
	/**
	 * 检查是否已经是好友
	 *
	 * @param uid
	 * @return
	 */
	public boolean isFirend(String uid, String fid) {
		int conut=iFriendsService.count(new QueryWrapper<Friends>().eq("friend", fid).eq("me", fid));
		if (conut==0) {
			return false;
		}
		return true;
	}
}
